﻿using System;
using CodeGenerator.Generator;
using TemplateManager;
using CodeGenerator.ProjectBL;

namespace CodeGenerator
{
    public class CodeGeneratorController: IGenerator
    {
        private readonly IGenerator _blGenerator;
        private readonly IGenerator _dalGenerator;
        private readonly IGenerator _webGenerator;
        private String _outputPath ="";
        public CodeGeneratorController(String outputPath)
        {
            var codeGenBL = new CodeGenBL.CodeGenBLClient();
            var projectBL = new ProjectBL.ProjectBLClient();
            _blGenerator = new BLGenerator(projectBL, codeGenBL, outputPath);
            _dalGenerator = new DALGenerator(projectBL, codeGenBL, outputPath);
            _webGenerator = new WebGenerator(projectBL, codeGenBL, outputPath);
            this._outputPath = outputPath;
        }

        public void Generate(int projectId)
        {
            _dalGenerator.Generate(projectId);
            _blGenerator.Generate(projectId);
            _webGenerator.Generate(projectId);
            GenerateSolution(projectId, _outputPath);
        }

        public IProject GenerateSolution(int projectId, string outputPath)
        {
            
            ProjectBLClient client = new ProjectBLClient();
            ProjectDto project = client.GetProject(new ProjectPkDto() { ProjectId = projectId }).Data;

            string projectName = project.Name;
            string solutionName = projectName;
            string mvcProjectName = projectName + ".Web";
            string blProjectName = projectName + ".Service";
            string dalProjectName = projectName + ".DAL";
            string templateDirectory = @"C:\test\Template\ComplexWebApplication1\";
            string primaryOutputDirectory = outputPath + @"\" + projectName + @"\";
            string completeProjectDirectory = outputPath + @"\Complete\";

            ComplexTemplate t = new ComplexTemplate();
            t.ProjectPath = templateDirectory;
            IProject p = t.CreateNewProject(solutionName, completeProjectDirectory,
                                            dalProjectName, blProjectName, mvcProjectName);
            p.AddFile(primaryOutputDirectory);
            return p;
        }
    }
}
